package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.model.Tdictionay;
import com.xy.core.service.TdictionayService;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Tsurveyoptions;
import com.xy.manage.model.Tsurveyquestion;
import com.xy.manage.service.TsurveyoptionsService;
import com.xy.manage.service.TsurveyquestionService;



@Controller
public class TsurveyquestionAction{
	private static final Logger log = LoggerFactory.getLogger(TsurveyquestionAction.class);
	public TsurveyquestionAction() {
	}
 
 
 	/**
	 * 问卷题目新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyquestionAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tsurveyquestion", new Tsurveyquestion());
        setDict(model);
		return "tsurveyquestionEdit";
	}

	/**
	 * 问卷题目新增保存
	 * 
	 * @param tsurveyquestion
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tsurveyquestionSave.do")
	public String save(Tsurveyquestion tsurveyquestion, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tsurveyquestion, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tsurveyquestion = tsurveyquestionService.save(tsurveyquestion);
		return "redirect:v_tsurveyquestionList.do";
	}

	/**
	 * 问卷题目删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tsurveyquestionDelete.do")
	public String delete(long[] ids) {
		Map<String,Object> map = new HashMap<String, Object>();
		
		if (ids != null) {
			for (long id : ids) {
				map.put("tsurveyquestion", tsurveyquestionService.get(id));
				List<Tsurveyoptions> list = tsurveyoptionsService.getTsurveyoptions(map);
				for(Tsurveyoptions t : list){
					tsurveyoptionsService.remove(t);
				}
				tsurveyquestionService.remove(id);
			}
		}
		return "redirect:v_tsurveyquestionList.do";
	}

	/**
	 * 问卷题目编辑页面
	 * 
	 * @param questionid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyquestionEdit.do")
	public String edit(long questionid, HttpServletRequest request, ModelMap model) {
		Tsurveyquestion tsurveyquestion = tsurveyquestionService.get(questionid);
		model.addAttribute("tsurveyquestion", tsurveyquestion);
      model.addAttribute("actionEdit", request.getParameter("actionEdit"));
        setDict(model);
		return "tsurveyquestionEdit";
	}

	/**
	 * 问卷题目详情
	 * 
	 * @param questionid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyquestionDetail.do")
	public String detail(long questionid, HttpServletRequest request, ModelMap model) {
		Tsurveyquestion tsurveyquestion = tsurveyquestionService.get(questionid);
		model.addAttribute("tsurveyquestion", tsurveyquestion);
//		Map<String,Object> map = new HashMap<String, Object>();
//		map.put("tsurveyquestion", tsurveyquestion);
//		model.addAttribute("tsurveyoptionses", tsurveyoptionsService.getTsurveyoptions(map));
        setDict(model);
		return "tsurveyquestionView";
	}

	/**
	 * 问卷题目查询（分页）
	 * 
	 * @param tsurveyquestion
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyquestionList.do")
	public String listPage(Tsurveyquestion tsurveyquestion, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tsurveyquestionService.getTsurveyquestionByPage(toMap(tsurveyquestion),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tsurveyquestionList";
	}

	/**
	 * 问卷题目查询
	 * 
	 * @param tsurveyquestion
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveyquestionList1.do")
	public String list(Tsurveyquestion tsurveyquestion, HttpServletRequest request, ModelMap model) {
		List<Tsurveyquestion> list = tsurveyquestionService.getTsurveyquestion(toMap(tsurveyquestion));
		model.addAttribute("list", list);
        setDict(model);
		return "tsurveyquestionList";
	}
 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tsurveyquestion
	 * @return
	 */	private Map<String,Object> toMap(Tsurveyquestion tsurveyquestion){
		Map<String,Object> map=new HashMap<String, Object>();
   		if(tsurveyquestion.getQuestionid()!=null&&!tsurveyquestion.getQuestionid().equals("")){
			map.put("questionid", tsurveyquestion.getQuestionid());
		}
		if(tsurveyquestion.getSurverytype()!=null&&!tsurveyquestion.getSurverytype().equals("")){
			map.put("surverytype", tsurveyquestion.getSurverytype());
		}
		if(tsurveyquestion.getQuestionseq()!=null&&!tsurveyquestion.getQuestionseq().equals("")){
			map.put("questionseq", tsurveyquestion.getQuestionseq());
		}
		if(tsurveyquestion.getQuestiontype()!=null&&!tsurveyquestion.getQuestiontype().equals("")){
			map.put("questiontype", tsurveyquestion.getQuestiontype());
		}
		if(tsurveyquestion.getQuestionname()!=null&&!tsurveyquestion.getQuestionname().equals("")){
			map.put("questionname", tsurveyquestion.getQuestionname());
		}
		if(tsurveyquestion.getBz()!=null&&!tsurveyquestion.getBz().equals("")){
			map.put("bz", tsurveyquestion.getBz());
		}
		if(tsurveyquestion.getOper()!=null&&!tsurveyquestion.getOper().equals("")){
			map.put("oper", tsurveyquestion.getOper());
		}
		if(tsurveyquestion.getModtime()!=null&&!tsurveyquestion.getModtime().equals("")){
			map.put("modtime", tsurveyquestion.getModtime());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("surverytypeMap", DictUtils.getDictByType(DictUtils.TSURVEYQUESTION_SURVERYTYPE));
      model.addAttribute("questiontypeMap", DictUtils.getDictByType(DictUtils.TSURVEYQUESTION_QUESTIONTYPE));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tsurveyquestion tsurveyquestion, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   

	@Resource
	private TsurveyquestionService tsurveyquestionService;  
	@Resource
	private TsurveyoptionsService tsurveyoptionsService;  
}